import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutterzhuyili/page/router.dart';
import 'package:flutterzhuyili/page/turnCard/cardPage.dart';
import 'package:flutterzhuyili/utils/shared_preferences_keys.dart';
import 'package:flutterzhuyili/utils/textMessage.dart';
import 'package:fsuper/fsuper.dart';

import '../main.dart';

class GameSelectPage extends StatefulWidget {
  @override
  _GameSelectPageState createState() => _GameSelectPageState();
}

class _GameSelectPageState extends State<GameSelectPage> {
  DateTime lastPopTime;

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      checkPrivacyPolicy();
    });
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
        child: Scaffold(
          appBar: AppBar(
            title: Text(
              '游戏选择',
              style: TextStyle(color: Colors.white),
            ),
            actions: <Widget>[
              IconButton(
                  icon: Icon(Icons.person),
                  onPressed: () {
                    Navigator.of(context).pushNamed(RouterMePage);
                  })
            ],
          ),
          body: Column(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: <Widget>[
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: <Widget>[
                  FSuper(
                    onClick: () {
                      Navigator.of(context).pushNamed(RouterShuerteMainPage);
                    },
                    width: 120,
                    height: 120,
                    textSize: 18,
                    corner: Corner.all(10),
                    textColor: Colors.white,
                    textAlignment: Alignment.center,
                    text: '舒尔特方格',
                    gradient: LinearGradient(
                        colors: [Colors.green, Colors.greenAccent]),
                  ),
                  FSuper(
                    onClick: () {
                      Navigator.of(context).pushNamed(RouterBallPage);
                    },
                    width: 120,
                    height: 120,
                    textSize: 18,
                    textColor: Colors.white,
                    textAlignment: Alignment.center,
                    gradient:
                        LinearGradient(colors: [Colors.red, Colors.redAccent]),
                    corner: Corner.all(10),
                    text: '滚球',
                  ),
                  FSuper(
                    onClick: () {
                      Navigator.of(context).push(new MaterialPageRoute(
                        builder: (parentContext) {
                          //指定跳转的页面
                          return CardPage(1);
                        },
                      ));
                    },
                    width: 120,
                    height: 120,
                    textSize: 18,
                    textColor: Colors.white,
                    textAlignment: Alignment.center,
                    gradient: LinearGradient(
                        colors: [Colors.lightBlue, Colors.lightBlueAccent]),
                    corner: Corner.all(10),
                    text: '翻牌',
                  )
                ],
              ),
            ],
          ),
        ),
        onWillPop: () async {
          if (lastPopTime == null ||
              DateTime.now().difference(lastPopTime) > Duration(seconds: 2)) {
            lastPopTime = DateTime.now();
            BotToast.showText(text: '再按一次退出');
            return false;
          } else {
            // 退出app
            return true;
          }
        });
  }

  checkPrivacyPolicy() async {
    if (sp.hasKey(SharedPreferencesKeys.agreePolicy) &&
        sp.getBool(SharedPreferencesKeys.agreePolicy) == true) {
      return;
    }
    showDialog(
        //点击空白 无法关闭
        barrierDismissible: false,
        context: context,
        builder: (context) {
          return WillPopScope(
            child: AlertDialog(
              title:
                  Text('个人隐私保护指引', style: TextStyle(color: Colors.blueAccent)),
              content: Container(
                child: SingleChildScrollView(
                  child: RichText(
                    text: TextSpan(children: [
                      TextSpan(
                          style: TextStyle(color: Colors.black),
                          text: '欢迎使用本产品，本产品非常重视您的隐私和个人信息。在您使用本产品前，请认真阅读'),
                      TextSpan(
                        text: '《用户协议》',
                        style: TextStyle(color: Colors.red),
                        recognizer: TapGestureRecognizer()
                          ..onTap = () {
                            Navigator.pushNamed(context, RouterInfomationPage,
                                arguments: {'title': '用户协议', 'message': xy});
                          },
                      ),
                      TextSpan(
                          text: '及', style: TextStyle(color: Colors.black)),
                      TextSpan(
                        text: '《隐私政策》',
                        style: TextStyle(color: Colors.red),
                        recognizer: TapGestureRecognizer()
                          ..onTap = () {
                            Navigator.pushNamed(context, RouterInfomationPage,
                                arguments: {'title': '隐私政策', 'message': ys});
                          },
                      ),
                      TextSpan(
                          text: '您接受并同意条款后即可开始使用本产品。',
                          style: TextStyle(color: Colors.black)),
                    ]),
                  ),
                ),
              ),
              actions: <Widget>[
                FlatButton(
                  onPressed: () async {
                    await SystemChannels.platform
                        .invokeMethod('SystemNavigator.pop');
                  },
                  child: Text(
                    '不同意并退出',
                    style: TextStyle(color: Colors.grey),
                  ),
                ),
                FlatButton(
                  onPressed: () {
                    sp.putBool(SharedPreferencesKeys.agreePolicy, true);
                    Navigator.of(context).pop();
                  },
                  child: Text(
                    '同意',
                    style: TextStyle(color: Colors.green),
                  ),
                )
              ],
            ),
            onWillPop: () async {
              return false;
            },
          );
        });
  }
}
